public class Solution {

    /**
     * 整数数组 nums 按升序排列，数组中的值 互不相同 。
     * 在传递给函数之前，nums 在预先未知的某个下标 k（0 <= k < nums.length）上进行了 旋转，
     * 使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]（下标 从 0 开始 计数）。
     * 例如， [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为[4,5,6,7,0,1,2] 。
     * 给你 旋转后 的数组 nums 和一个整数 target ，如果 nums 中存在这个目标值 target ，则返回它的下标，
     * 否则返回-1。
     * 你必须设计一个时间复杂度为 O(log n) 的算法解决此问题。
     */
    public static int search(int[] nums, int target) {
        return rotateSearch(nums, 0, nums.length - 1, target);
    }

    public static int rotateSearch(int[] arrays, int start, int end, int target) {
        int length;
        while ((length = end - start) > 6) {
            int mid_1 = start + length / 3;
            int mid_2 = mid_1 + length / 3;

            int startValue = arrays[start];
            int mid1Value = arrays[mid_1];
            int mid2Value = arrays[mid_2];

            if (target > startValue) {
                if (mid2Value > mid1Value) {
                    if (mid1Value > startValue) {
                        if (target <= mid1Value) {
                            return ascSearch(arrays, start, mid_1, target);
                        } else if (target <= mid2Value) {
                            return ascSearch(arrays, mid_1, mid_2, target);
                        } else {
                            return rotateSearch(arrays, mid_2, end, target);
                        }
                    } else {
                        return rotateSearch(arrays, start, mid_1, target);
                    }
                } else {
                    if (target <= mid1Value) {
                        return ascSearch(arrays, start, mid_1, target);
                    } else {
                        return rotateSearch(arrays, mid_1, mid_2, target);
                    }
                }
            } else if (target < startValue) {
                if (mid2Value > mid1Value) {
                    if (mid1Value > startValue) {
                        return rotateSearch(arrays, mid_2, end, target);
                    } else {
                        if (target <= mid1Value) {
                            return rotateSearch(arrays, start, mid_1, target);
                        } else if (target <= mid2Value) {
                            return ascSearch(arrays, mid_1, mid_2, target);
                        } else {
                            return ascSearch(arrays, mid_2, end, target);
                        }
                    }
                } else {
                    if (target >= mid2Value) {
                        return ascSearch(arrays, mid_2, end, target);
                    } else {
                        return rotateSearch(arrays, mid_1, mid_2, target);
                    }
                }
            } else {
                return start;
            }
        }

        for (int i = start; i <= end; i++) {
            if (arrays[i] == target) {
                return i;
            }
        }

        return -1;
    }

    public static int ascSearch(int[] arrays, int start, int end, int target) {
        int length;
        while ((length = end - start) > 6) {
            int mid = start + length / 2;
            int value = arrays[mid];
            if (value > target) {
                end = mid;
            } else if (value < target) {
                start = mid;
            } else {
                return mid;
            }
        }

        for (int i = start; i <= end; i++) {
            if (arrays[i] == target) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arrays = new int[]{12,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        System.out.println(search(arrays, -130));
    }


}